﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Regions;
using EIA.SLClient.PlatManage.Infrastructure;

namespace EIA.SLClient.PlatManage.BasicMgr
{
    /// <summary>
    /// BasicMgrNavigationView.xaml 的交互逻辑
    /// </summary>
    [Export("EIA.SLClient.PlatManage.BasicMgr.BasicMgrNavigationView")]
    public partial class BasicMgrNavigationView : UserControl, IPartImportsSatisfiedNotification
    {

        private static Uri departmentViewUri = new Uri(typeof(DepartmentView).FullName, UriKind.Relative);
        private static Uri positionViewUri = new Uri(typeof(PositionView).FullName, UriKind.Relative);
        private static Uri staffViewUri = new Uri(typeof(StaffView).FullName, UriKind.Relative);

        public BasicMgrNavigationView()
        {
            this.InitializeComponent();

            // 在此点之下插入创建对象所需的代码。
            this.Loaded += (s, e) => { UpdateMainRegion(); };
        }
        [Import]
        public IRegionManager regionManager;

        void IPartImportsSatisfiedNotification.OnImportsSatisfied()
        {
            IRegion mainContentRegion = this.regionManager.Regions[RegionNames.MainContentRegion];
            if (mainContentRegion != null && mainContentRegion.NavigationService != null)
            {
                mainContentRegion.NavigationService.Navigated += this.MainContentRegion_Navigated;
            }
        }

        public void MainContentRegion_Navigated(object sender, RegionNavigationEventArgs e)
        {
            this.UpdateNavigationButtonState(e.Uri);
        }

        private void UpdateNavigationButtonState(Uri uri)
        {
            if (uri == departmentViewUri)
            {
                this.btn_department.IsChecked = true;
            }
            else if (uri == positionViewUri)
            {
                this.btn_position.IsChecked = true;
            }
            else if (uri == staffViewUri)
            {
                this.btn_staff.IsChecked = true;
            }
        }

        private void UpdateMainRegion()
        {
            if (this.btn_department.IsChecked == true)
            {
                this.regionManager.RequestNavigate(RegionNames.MainContentRegion, departmentViewUri);
            }
            else if (this.btn_position.IsChecked == true)
            {
                this.regionManager.RequestNavigate(RegionNames.MainContentRegion, positionViewUri);
            }
            else if (this.btn_staff.IsChecked == true)
            {
                this.regionManager.RequestNavigate(RegionNames.MainContentRegion, staffViewUri);
            }
            else
            {
                this.regionManager.RequestNavigate(RegionNames.MainContentRegion, departmentViewUri);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            UpdateMainRegion();
        }
    }
}